<?php // called via AJAX
class Calendar extends Controller {
    private $eventList = array(); // date,time,desc
    public function __construct() {
            parent::Controller();
            $this->load->helper('url');
            $this->load->model("EventsModel");
            $this->load->helper("comfunc");
        }
    private function addEvent($id,$date,$time,$desc){
            $this->eventList[count($this->eventList)] = array("id"=>$id,"date"=>$date,"time"=>$time,"desc"=>$desc);
        }
    private function getEventList($start,$end){
            $calName = "WAHA"; // ideally the name of the calendar
            $eventFeed = $this->EventsModel->getEventsList($calName,$start,$end);
            return $eventFeed;
        }
    public function display() {
            $output = '';
            $month = intval($this->uri->segment(3));
            $year = intval($this->uri->segment(4));
            if($month == '' || $year == '') {
                    $time = time();
                    $month = date('n',$time);
                    $year = date('Y',$time);
                }
            $date = getdate(mktime(0,0,0,$month,1,$year));
            $today = getdate();
            $hours = $today['hours'];
            $mins = $today['minutes'];
            $secs = $today['seconds'];
        
                if(strlen($hours)<2) $hours="0".$hours;
                if(strlen($mins)<2) $mins="0".$mins;
                if(strlen($secs)<2) $secs="0".$secs;
        
            $days=date("t",mktime(0,0,0,$month,1,$year));
            $start = $date['wday']+1;
            $name = $date['month'];
            $year2 = $date['year'];
            $offset = $days + $start - 1;
        
            if($month==12) {
                    $next=1;
                    $nexty=$year + 1;
                } else {
                    $next=$month + 1;
                    $nexty=$year;
                }
        
            if($month==1) {
                    $prev=12;
                    $prevy=$year - 1;
                } else {
                    $prev=$month - 1;
                    $prevy=$year;
                }
        
                if($offset <= 28) $weeks=28;
                    elseif($offset > 35) $weeks = 42;
                    else $weeks = 35;
        
            $endDate = date("Y-m-d",mktime(0,0,0,$month+1,1,$year));
            $eventsList = $this->getEventList($year . "-" . ((strlen($month)>1)?$month:"0".$month) . "-01",$endDate);
            $output .= "
                <table class='cal' cellspacing='1'>
                <tr>
                <td colspan='7'>
                <table class='calhead'>
                <tr>
                <td>
                <a href='javascript:navigate($prev,$prevy)'><img src='/images/calendar/calLeft.gif'></a> <a href='javascript:navigate(\"\",\"\")'><img src='/images/calendar/calCenter.gif'></a> <a href='javascript:navigate($next,$nexty)'><img src='/images/calendar/calRight.gif'></a>
                </td>
                <td align='right'>
                <div>$name $year2</div>
                </td>
                </tr>
                </table>
                </td>
                </tr>
                <tr class='dayhead'>
                <td>Sun</td>
                <td>Mon</td>
                <td>Tue</td>
                <td>Wed</td>
                <td>Thu</td>
                <td>Fri</td>
                <td>Sat</td>
                </tr>";
            $col=1;
            $cur=1;
            $next=0;
        
            for($i=1;$i<=$weeks;$i++) {
                        if($next==3) $next=0;
                        if($col==1) $output.="<tr class='dayrow'>";
                    $output.="<td valign='top' onMouseOver=\"this.className='dayover'\" onMouseOut=\"this.className='dayout'\"";
                    if($i <= ($days+($start-1)) && $i >= $start) {
                            $currDate = $year . "-" . ((strlen($month )<2)?"0".$month :$month). "-" . ((strlen($cur)<2)?"0".$cur:$cur);
                            $hasEvent = $this->getEvent($eventsList,$currDate);
                            if($hasEvent=="") {
                                    $output.=" onclick=\"return createEventPopup('" . $currDate . "',event);\"";
                                }
                            $output.=">";
                            $output.="<div class='day'>";
                            $output.="<strong";
                            if(($cur==$today['mday']) && ($name==$today['month'])) {
                                    $output.=" style='color:#C00'";
                                }
                            $output.=">" . $cur;
                            if($hasEvent!="") {
                                    $output.=$hasEvent;
                                }
                            $output.= "</strong></div></td>";
                            $cur++;
                            $col++;
                        } else {
                            $output.="&nbsp;</td>";
                            $col++;
                        }
                
                    if($col==8) {
                            $output.="</tr>";
                            $col=1;
                        }
                }
            $output.="</table>";
            print $output;
        }
        /**
        * Checks to see if event is in the EventList param, if so, constructs anchor link
        *
        * @param array $eventList
        * @param date $date
        * @return string
        */
    private function getEvent($eventList,$date){
            $retVal = null;
            for($i=0;$i<count($eventList);$i++){
                    if($eventList[$i]["startDate"]==$date) {
                            $retVal .= "<br/><a href=\"javascript:void(0);\" onclick=\"return showEvent('" . $eventList[$i]["id"] . "',event);\">" . $eventList[$i]["title"] . "</a>";
                        }
                }
            return $retVal;
        }
        /**
        * views specific event by ID
        *
        * @param string $id
        */
    public function event($id) {
            $output = array();//array("error"=>"<h2>Event '" . $id . "' Not Found</h2>"); //setting default return
            $eventId = $this->uri->segment(3);
            $eventList = $this->EventsModel->getEventById($id);
            for($i=0;$i<count($eventList);$i++){
                    if(stristr($id,$eventList[$i]["id"])>0) {
                            array_push($output,array(
                                "id"=>$eventList[$i]["id"],
                                "title"=>$eventList[$i]["title"],
                                "startDate"=>$eventList[$i]["startDate"],
                                "startTime"=>$eventList[$i]["startTime"],
                                "endDate"=>$eventList[$i]["endDate"],
                                "endTime"=>$eventList[$i]["endTime"],
                                "short_description"=>$eventList[$i]["short_description"],
                                "long_description"=>$eventList[$i]["long_description"],
                                "web_link"=>$eventList[$i]["web_link"]
                                )
                                );
                        }
                }
            print json_encode($output);
        }
}
?>